﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WP09S03_OfficeTracking.Controller;

namespace WP09S03_OfficeTracking
{
    public partial class FrmSuppliesReplenish : WP09S03_OfficeTracking.ParentClass.ParentForm
    {
        TransactionController tran = new TransactionController();
        ItemController itemCtrl = new ItemController();
        public FrmSuppliesReplenish()
        {
            InitializeComponent();
            InitSetting();
        }

        private void InitSetting()
        {
            this.ctrlOffice.BindData_Category(1);
            this.ctrlOffice.dgvData.MultiSelect = false;
        }
        private void btn_Click(object sender, EventArgs e)
        {
            try
            {
                string itemID = this.txtItemID.Text.Trim();
                if (itemID == "")
                {
                    SetMessage("Please input valid itemID.");
                    return;
                }

                if (itemCtrl.CheckItemID(itemID) == 0)
                {
                    SetMessage("Please input valid itemID.");
                    return;
                }


                int quantity = Convert.ToInt32(this.txtQuantity.Text.Trim());
                bool result = tran.ReplenishTransaction(itemID, quantity);
                if (result)
                {
                    clearTransactionInfo();
                    SetMessage("Transaction is successful.");
                }
                else
                {
                    SetMessage("Transaction is fail.");
                }
            }
            catch (Exception err)
            {
                SetMessage("Transaction is fail." + err.Message );
                CatchError(err);
            }

        }

        private void btnItemSelect_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.ctrlOffice.dgvData.SelectedRows.Count == 0)
                {
                    SetMessage("Pleaes select Item information.");
                    return;
                }
                else
                {
                    SetMessage("");
                }

                this.txtItemID.Text = this.ctrlOffice.dgvData.SelectedRows[0].Cells[0].Value.ToString();
                IsEnableSubmit();
            }
            catch (Exception err)
            {
                CatchError(err);
            }
        }

        private void txtQuantity_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if (!(Char.IsNumber(e.KeyChar) || e.KeyChar == '\b'))
                {
                    e.Handled = true;
                }
            }
            catch (Exception err)
            {
                CatchError(err);
            }

        }

        private void clearTransactionInfo()
        {
            this.txtItemID.Text = "";
            this.txtQuantity.Text = "";
            this.ctrlOffice.BindData_DGV();

        }

        private void FrmSuppliesReplenish_Load(object sender, EventArgs e)
        {
            this.btnItemSelect.Enabled = false;
            IsEnableSubmit();
            this.ctrlOffice.dgvData.SelectionChanged += dgvData_SelectionChanged;

        }

        private void dgvData_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.ctrlOffice.dgvData.SelectedRows.Count > 0)
                {
                    btnItemSelect.Enabled = true;
                }
                else
                {
                    btnItemSelect.Enabled = false;
                }
            }
            catch(Exception err)
            {
                CatchError(err);
            }
        }

        private void IsEnableSubmit()
        {
            if (this.txtItemID.Text.Trim() != "")
            {
                this.btnSubmit.Enabled = true;
            }
            else
            {
                this.btnSubmit.Enabled = false;
            }
        }
    }
}
